//
//  PasswordLoginViewController.swift
//  SwiftPanda
//
//  Created by 余意 on 2017/3/1.
//  Copyright © 2017年 余意. All rights reserved.
//

import UIKit
import PKHUD

class PasswordLoginViewController: UIViewController {
    
    let containerSV = UIScrollView()
    let containerView = UIView()
    let headIV = UIImageView()
    let whiteView = UIView()
    let mobileTF = UITextField()
    let passwordTF = UITextField()
    let loginBtn = UIButton()
    
    var mobile = String()
    var password = String()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "密码登录"
        self.view.backgroundColor = BACKGROUND_COLOR
        
        setupUI()
        
        mobileTF.text = "18521090832"
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func loginBtnClick(sender:UIButton) {
        mobile = mobileTF.text!
        if mobile.isEmpty {
            HUD.flash(.label("请填写手机号"), delay: 1.0)
            return
        }
        
        password = passwordTF.text!
        if password.isEmpty {
            HUD.dimsBackground = false
            HUD.flash(.label("请填写密码"), delay: 1.0)
            return
        }
        
        let urlString = "http://121.40.121.237:4400/api/login"
        let dic = ["mobile":mobile,
                   "password":password]
        
        NetworkTools.requestData(.post, URLString: urlString, parameters: dic) { (result) in
            if result["code"].string == "000" {
                print(result)
                //保存token mobile
                
                if let token = result["data"]["token"].string {
                    saveUserInfo(key: "token", token)
                }
                
                saveUserInfo(key: "mobile", self.mobile)
                
                print(getUserInfo(.token))
                print(getUserInfo(.mobile))
                
                self.dismiss(animated: true, completion: nil)
                
            }
        }
    }
    
    func setupUI() {
        self.view.addSubview(containerSV)
        containerSV.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        containerSV.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.containerSV)
            make.width.equalTo(self.containerSV)
        }
        
        headIV.layer.cornerRadius = 40
        headIV.layer.masksToBounds = true
        headIV.backgroundColor = UIColor.red
        containerView.addSubview(headIV)
        headIV.snp.makeConstraints { (make) in
            make.top.equalTo(containerView).offset(16)
            make.width.equalTo(80)
            make.height.equalTo(80)
            make.centerX.equalTo(containerView)
        }
        
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = 4
        whiteView.layer.masksToBounds = true
        containerView.addSubview(whiteView)
        whiteView.snp.makeConstraints { (make) in
            make.top.equalTo(headIV.snp.bottom).offset(12)
            make.left.equalTo(containerView).offset(8)
            make.right.equalTo(containerView).offset(-8)
            make.height.equalTo(100.5)
        }
        
        let view = UIView()
        whiteView.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(whiteView)
            make.height.equalTo(50)
        }
        
        let mobileLab = UILabel()
        mobileLab.text = "用户名"
        mobileLab.textColor = TEXT_BLACK_COLOR
        mobileLab.font = UIFont.systemFont(ofSize: 16)
        whiteView.addSubview(mobileLab)
        mobileLab.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(16)
            make.centerY.equalTo(view)
        }
        
        let mobileline = UIImageView()
        mobileline.image = UIImage(named:"line_vertiacl")
        whiteView.addSubview(mobileline)
        mobileline.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(75)
            make.centerY.equalTo(view)
            make.top.equalTo(view).offset(8)
            make.width.equalTo(0.5)
        }
        
        whiteView.addSubview(mobileTF)
        mobileTF.keyboardType = .numberPad
        mobileTF.clearButtonMode = .whileEditing
        mobileTF.placeholder = "请输入手机号"
        mobileTF.font = UIFont.systemFont(ofSize: 14)
        mobileTF.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(85)
            make.right.equalTo(whiteView)
            make.centerY.equalTo(view)
        }
        
        let longline = UIImageView()
        longline.image = UIImage(named:"line_gray")
        whiteView.addSubview(longline)
        longline.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView).offset(50)
            make.left.right.equalTo(whiteView)
            make.height.equalTo(0.5)
        }
        
        let passwordLab = UILabel()
        passwordLab.text = "密码"
        passwordLab.font = UIFont.systemFont(ofSize: 16)
        passwordLab.textColor = TEXT_BLACK_COLOR
        whiteView.addSubview(passwordLab)
        passwordLab.snp.makeConstraints { (make) in
            make.centerY.equalTo(mobileLab).offset(50.5)
            make.left.equalTo(mobileLab)
        }
        
        let codeline = UIImageView()
        codeline.image = UIImage(named:"line_vertical")
        whiteView.addSubview(codeline)
        codeline.snp.makeConstraints { (make) in
            make.centerY.equalTo(mobileline).offset(50.5)
            make.centerX.equalTo(mobileline)
            make.size.equalTo(mobileline)
        }
        
        whiteView.addSubview(passwordTF)
        passwordTF.clearButtonMode = .whileEditing
        passwordTF.placeholder = "请输入密码"
        passwordTF.font = UIFont.systemFont(ofSize: 14)
        passwordTF.snp.makeConstraints { (make) in
            make.left.equalTo(mobileTF)
            make.right.equalTo(mobileTF)
            make.centerY.equalTo(passwordLab)
        }
        
        containerView.addSubview(loginBtn)
        loginBtn.addTarget(self, action: #selector(loginBtnClick(sender:)), for: .touchUpInside)
        loginBtn.setBackgroundImage(UIImage(named:"defalut_btn"), for: .normal)
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView.snp.bottom).offset(16)
            make.left.equalTo(containerView).offset(16)
            make.right.equalTo(containerView).offset(-16)
            make.height.equalTo(45)
            make.bottom.equalTo(containerView).offset(-20)
        }
        
    }
    
   

}
